package com.example.adminbase.shiro;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.servlet.AdviceFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;

public class LoginSuccessAfterFilter extends AdviceFilter {

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        if(WebUtils.toHttp(request).getMethod().equalsIgnoreCase(AccessControlFilter.POST_METHOD)) {
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.write(JSON.toJSONString(SecurityUtils.getSubject().getSession().getAttribute("user")));
            return false;
        }else { //访问登录页面
            return true;
        }
    }
}
